var net = require('net');

var server = net.createServer(function (client) {
	console.log('Client connection: ');
	console.log('local = %s:%s', this.localAddress, this.localPort);
	console.log('remote = %s:%s', this.remoteAddress, this.remotePort);
	this.setTimeout(500);
	this.setEncoding('utf8');

	client.on('data', function(data) {
		console.log('Recevived data from client on port %d:%s', client.remotePort, data.toString());
		console.log('Bytes received:' + client.bytesRead);
		writeData(client, 'Sending:' + data.toString());
		console.log('Bytes sent:' + client.bytesWritten);
	});

	client.on('end', function() {
		console.log('Client disconnected!');
		server.getConnections(function (err, count) {
			console.log('Remaining Connections: ' + count);
		});
	});
	client.on('error', function(error) {
		console.log('Socket Error :',JSON.stringify(error));
	});

	client.on('timeout', function() {
		console.log('timeout!');
	});
});

server.listen(10000, function () {
	console.log('Server listening: ' + JSON.stringify(server.address()));
	server.on('close', function() {
		console.log('Server Terminated');
	});

	server.on('error', function(err) {
		console.log('Server Error', JSON.stringify(err));
	});
});

function writeData (socket, data) {
	var success = !socket.write(data);
	if (!success) {
		(function (socket, data) {
			socket.once('drain', function () {
				writeData(socket, data);
			});
		})(socket, data);
	}
}